<template>
  <div>
    <el-dialog class="bg-white-dialog" :visible.sync="showDialog" width="1190px" :close-on-click-modal="false" :close-on-press-escape="false">
      <div slot="title">
        <div class="flex justify-between align-center padding-right-30">
          <div>
            <span class="color-333333 font-size-18 font-weight-700 padding-right-20">{{ title }}</span>
            <span class="color-666666">单位：万元</span>
          </div>
          <div>
            <span class="color-666666 padding-right-20">财年&nbsp;{{ fiscalYear }}</span>
            <span class="color-666666">日期&nbsp;{{ fiscalYear }}-{{ fiscalMonth < 10 ? '0' + fiscalMonth : fiscalMonth }}-{{ fiscalDay }}</span>
          </div>
        </div>
      </div>

      <dialog-more-table :tableData="tableGroupData" :dialogType="dialogType" :fiscalYear="fiscalYear" :fiscalMonth="fiscalMonth" :clickMonth="clickMonth" :showHeader="true" class="margin-bottom-10"></dialog-more-table>
      <dialog-more-table :tableData="tableData" :dialogType="dialogType" :fiscalYear="fiscalYear" :fiscalMonth="fiscalMonth" :clickMonth="clickMonth" :showHeader="false"></dialog-more-table>
    </el-dialog>
  </div>
</template>
<script>
  import { apiHomeCashPopStatistics, apiPerformanceDetail } from '@a/about';
  import { goOperationIndex } from '@/common/js/utils';
  import dialogMoreTable from './dialogMoreTable.vue';

  import dayjs from 'dayjs';

  export default {
    props: {
      selectTime: {
        type: Number,
        default: 0,
      },
      dialogType: {
        type: Number,
        default: 1,
      },
      clickMonth: {
        type: Number,
        default: 1,
      },
      universalParams: {
        type: Object,
        default: () => {
          return {};
        },
      },
    },

    computed: {
      fiscalYear() {
        return dayjs(this.selectTime).format('YYYY');
      },
      fiscalMonth() {
        let date = dayjs(this.selectTime).format('YYYY-MM-DD');
        let month = this.dialogType == 7 ? this.clickMonth : date.split('-')[1];
        return Number(month);
      },
      fiscalDay() {
        let day = new Date(new Date(this.fiscalYear, this.fiscalMonth).setDate(0)).getDate();
        return day;
      },
      title() {
        if (this.dialogType == 7) {
          return this.clickMonth + '月净现金流' + (this.fiscalMonth < this.clickMonth ? '预测' : '');
        }
        if (this.dialogType == 8) {
          return this.dutyType == "income" ? "累计权责收入" : this.dutyType == "pay" ? "累计权责支出" : "累计净利润"
        }
        let list = ['累计现金流入', '全年现金流入预测', '累计支出', '全年支出预测', '累计净现金流', '全年净现金流预测'];
        return list[this.dialogType - 1];
      },
    },

    data() {
      return {
        isIncome: false,
        showDialog: false,
        tableData: [],
        tableGroupData: [],
        goOperationIndex: goOperationIndex,
        dutyType:"",//判断权责的类型
      };
    },

    methods: {
      init(dutyType) {
        this.dutyType = dutyType
        if (this.dialogType == 8) {
          this.getPerformanceDetails();
        } else {
          this.getHomeCashPopStatistics();
        }
        this.showDialog = true;
      },

      //上面6个现金流的弹窗数据源
      //统计类型 1-流入执行 2-流入全年预测 3-支出执行 4-支出全年预测 5-净现金流 6-净现金流全年预测 7-月净现金流
      async getHomeCashPopStatistics() {
        let param = this.lodash.cloneDeep(this.universalParams);
        if (this.dialogType == 7) param.month = this.clickMonth;
        param.type = this.dialogType;
        // console.log(this.dialogType, this.clickMonth, param)
        let res = await apiHomeCashPopStatistics(param);
        this.setDiffData(res.rows);
        this.tableGroupData = [res.rows[0]];
        res.rows.splice(0, 1);
        this.tableData = res.rows || [];
      },

      async getPerformanceDetails() {
        let res = await apiPerformanceDetail(this.universalParams);
        let sourceList = ['planAmount', 'executionAmount', 'deviationAmount', 'deviationRate', 'operationPlanAmount', 'operationExecutionAmount', 'operationDeviationAmount', 'operationDeviationRate', 'nonOperationPlanAmount', 'nonOperationExecutionAmount', 'nonOperationDeviationAmount', 'nonOperationDeviationRate'];
        let targetList = ['netPlanIncomeTotal', 'netIncomeTotal', 'netIncomeDeviation', 'netIncomeDeviationRate', 'forecastPlanIncomeTotal', 'forecastIncomeTotal', 'incomeDeviation', 'incomeDeviationRate', 'forecastPlanExpendTotal', 'forecastExpendTotal', 'expendDeviation', 'expendDeviationRate'];
        res.rows.forEach(item => {
          for (let i in sourceList) {
            item[sourceList[i]] = item[targetList[i]];
          }
        });
        this.setDiffData(res.rows);
        this.tableGroupData = [res.rows[0]];
        res.rows.splice(0, 1);
        this.tableData = res.rows || [];
      },

      //差异值的设置
      setDiffData(list) {
        list.forEach(item => {
          if (item.executionAmount == 0 && item.planAmount == 0) {
            item.deviationRate = '--';
          } else if (Number(item.executionAmount) != 0 && item.planAmount == 0) {
            item.deviationRate = '计划外';
          }

          if (item.operationExecutionAmount == 0 && item.operationPlanAmount == 0) {
            item.operationDeviationRate = '--';
          } else if (Number(item.operationExecutionAmount) != 0 && item.operationPlanAmount == 0) {
            item.operationDeviationRate = '计划外';
          }

          if (item.nonOperationExecutionAmount == 0 && item.nonOperationPlanAmount == 0) {
            item.nonOperationDeviationRate = '--';
          } else if (Number(item.nonOperationExecutionAmount) != 0 && item.nonOperationPlanAmount == 0) {
            item.nonOperationDeviationRate = '计划外';
          }
        });
      },
    },

    components: { dialogMoreTable },
  };
</script>

<style lang="scss" scoped>
  ::v-deep .el-table {
    .head1 {
      background: rgba(247, 145, 0, 0.25) !important;
    }
    .head2 {
      background: rgba(247, 145, 0, 0.1) !important;
    }
    .head3 {
      background: #fef4e5 !important;
    }
    .cell {
      padding: 0 !important;
    }
  }
</style>
